package com.example.config.security;

import com.alibaba.fastjson.JSON;
import com.example.common.ResultBody;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
public class DenyHandle implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        response.setCharacterEncoding("utf8");
        response.setContentType("application/json");
        response.setStatus(405);
        ResultBody result = new ResultBody(false, null, "没有权限");
        response.getWriter().println(JSON.toJSONString(result));
    }
}
